package com.xuecheng.content.api;

import com.alibaba.fastjson.JSON;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.model.vo.CourseBaseInfoVo;
import com.xuecheng.content.model.vo.CoursePreviewVo;
import com.xuecheng.content.model.vo.TeachplanVo;
import com.xuecheng.content.service.CoursePublishService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CoursePublishController {

    @Autowired
    private CoursePublishService coursePublishService;

    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView();
        // 查询课程的信息作为模型数据
        CoursePreviewVo coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        // 指定模型
        modelAndView.addObject("model", coursePreviewInfo);
        // 指定模板
        modelAndView.setViewName("course_template"); // 根据视图名称加.ftl找到模板
        return modelAndView;
    }

    @ResponseBody
    @PostMapping ("/courseaudit/commit/{courseId}")
    public void commitAudit(@PathVariable("courseId") Long courseId){
        Long companyId = 1232141425L;
        coursePublishService.commitAudit(companyId, courseId);
    }

    @ResponseBody
    @PostMapping ("/coursepublish/{courseId}")
    public void coursepublish(@PathVariable("courseId") Long courseId){
        Long companyId = 1232141425L;
        coursePublishService.publish(companyId, courseId);
    }

    @ResponseBody
    @GetMapping("/r/coursepublish/{courseId}")
    @ApiOperation("查询课程发布信息")
    public CoursePublish getCoursepublish(@PathVariable("courseId") Long courseId) {
        // 查询课程发布信息
        CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);
        return coursePublish;
    }

    @ResponseBody
    @GetMapping("/course/whole/{courseId}")
    @ApiOperation("获取课程发布信息")
    public CoursePreviewVo getCoursePublish(@PathVariable("courseId") Long courseId) {

        // 封装数据
        CoursePreviewVo coursePreviewVo = new CoursePreviewVo();

        // 查询课程发布表
        // CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);
        // 先从缓存查询，缓存中有直接返回，没有再查询数据库
        CoursePublish coursePublish = coursePublishService.getCoursePublishCache(courseId);
        if(coursePublish == null) {
            return coursePreviewVo;
        }

        // 开始向coursePreviewVo填充数据
        CourseBaseInfoVo courseBaseInfoVo = new CourseBaseInfoVo();
        BeanUtils.copyProperties(coursePublish, courseBaseInfoVo);
        // 课程计划信息
        String teachplanJson = coursePublish.getTeachplan();
        // 转成List<TeachplanVo>
        List<TeachplanVo> teachplanVos = JSON.parseArray(teachplanJson, TeachplanVo.class);
        coursePreviewVo.setCourseBase(courseBaseInfoVo);
        coursePreviewVo.setTeachplans(teachplanVos);
        return coursePreviewVo;
    }
}
